<?php

namespace app\admin\controller;

use app\admin\service\IllegalApiService;
use think\Request;
use think\Validate;

class IllegalApi extends Backend
{
    /**
     * @var IllegalApiService
     */
    protected $service;

    protected function initialize()
    {
        parent::initialize();
        $this->service = new IllegalApiService();
    }

    public function notice(Request $request)
    {

    }

    public function total(Request $request)
    {
        $param = $request->post();
        $data  = $this->service->total($this->userId, $param);
        $this->jsonReturn(message('success', true, $data));
    }

    public function lists(Request $request)
    {
        $param = $request->post();
        $data  = $this->service->lists($this->userId, $param);
        $this->jsonReturn(message('success', true, $data));
    }

    public function detail(Request $request)
    {
        $id = $request->post('id');
        if (strpos($id, '_') !== false) {
            $idArr  = explode('_', $id);
            $id     = $idArr[0];
            $userId = $idArr[1];
        } else {
            $userId = $this->userId;
        }
        $data = $this->service->detail($id, $userId);
        $this->jsonReturn(message('success', true, $data));
    }

    public function read(Request $request)
    {
        $id = $request->post('id');
        if (strpos($id, '_') !== false) {
            $idArr  = explode('_', $id);
            $id     = $idArr[0];
            $userId = $idArr[1];
        } else {
            $userId = $this->userId;
        }
        $data = $this->service->read($id, $userId);
        $this->jsonReturn(message('success', true, $data));
    }

    public function save(Request $request)
    {
        $param    = $request->post();
        $validate = Validate::make(
            [
                'id'  => 'require|integer',
                'res' => 'require|in:1,2'
            ]
        );
        if (!$validate->check($param)) {
            $this->jsonReturn(message($validate->getError(), false));
        }
        $res = $this->service->save($this->userId, $param);
        if ($res === true) {
            $this->jsonReturn(message('success'));
        } else {
            $this->jsonReturn(message($res, false));
        }
    }

    public function statistics()
    {
        $data = $this->service->statistics($this->userId);
        $this->jsonReturn(message('success', true, $data));
    }

    public function my(Request $request)
    {
        $param = $request->post();
        $data  = $this->service->my($this->userId, $param);
        $this->jsonReturn(message('success', true, $data));
    }
}
